﻿using UnityEngine;
using System;
using System.Collections;
using UnityEditor;
using DICE_GS;

public class DiceMenuWindow : EditorWindow {
    [MenuItem("TheDice/Open ImportTool")]
    static void Init()
    {
        EditorWindow.GetWindow<DiceMenuWindow>();
		mapList = Enum.GetNames(typeof(MapList));
		mapName = mapList[0];
		themeStrList = Enum.GetNames( typeof(element) );
		themeList = new element[themeStrList.Length];

		int i=0;
		foreach(element elem in Enum.GetValues(typeof(element)) )
		{
			themeList[i] = elem;
			i++;
		}
    }
	
	
	static string mapName;
	static string pathIndex = "0";
    static string [] mapList;
	static string [] themeStrList;
	static element [] themeList;
	int mapIndex = 0;
	int themeIndex = 0;
	bool exportOK=false;
    void OnGUI() {
		
		/*
        Event evt = Event.current;
        Rect contextRect  = new Rect (10, 10, 100, 100);
         
        if (evt.type == EventType.ContextClick)
        {
            Vector2 mousePos  = evt.mousePosition;
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("MenuItem1"), false, new GenericMenu.MenuFunction2(MenuArg1), "Menu Item 1");
            menu.AddItem(new GUIContent("MenuItem2"), false, new GenericMenu.MenuFunction2(MenuArg1), "Menu Item 2");
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, new GenericMenu.MenuFunction(MenuNoArg));
            menu.ShowAsContext();
            evt.Use();
        }*/
		
		
		int prevMapIndex = mapIndex;
		mapIndex = EditorGUILayout.Popup(mapIndex, mapList);
		if( mapIndex != prevMapIndex)
			mapName = mapList[mapIndex];
		
		int prevThemeIndex = themeIndex;
		themeIndex = EditorGUILayout.Popup(themeIndex, themeStrList);



		foreach( string nameMap in mapList)
		{
			string fullNameMap = "map_"+nameMap;
			if( themeList[themeIndex] != element.none)
				fullNameMap += String.Format("_"+themeList[themeIndex].ToString() );
			GameObject mapObj = GameObject.Find ( fullNameMap );
			if(mapObj!=null)
			{
				mapName = nameMap;
				break;
			}
		}
		
		//GUILayout.Label("Map Name", EditorStyles.boldLabel);
		mapName = EditorGUILayout.TextField("Map Name", mapName);
				
		if( GUI.Button(new Rect(150, 100, 100, 30), "맵 임포트") )
		{
			ExportManager.ImportMapName(mapName, themeList[themeIndex]);
		}
		if( GUI.Button(new Rect(350, 100, 100, 30), "맵 임포트(미니맵)") )
		{
			ExportManager.ImportMiniMapName(mapName);
		}
	
//		if( GUI.Button(new Rect(150, 100, 100, 30), "Import path") )
//		{
//			ExportManager.ImportPath(mapName);
//		}

		pathIndex = EditorGUILayout.TextField("PathIndex", pathIndex);
		if( GUI.Button(new Rect(150, 150, 120, 50), "goal까지 거리 계산\n(path 만든후 사용)") )
		{
			ExportManager.CalcutateRatioGoal( Int32.Parse(pathIndex) );
		}			
		if( GUI.Button(new Rect(150, 230, 100, 30), "xml로 익스포트") )
		{
			if(mapName=="none")
				exportOK=false;
			else
				exportOK = ExportManager.ExportMap(mapName);
		}
		if(exportOK)
			GUI.Label( new Rect(150, 280, 300, 30), String.Format("{0} export Success!",mapName) );
		//else
			//GUI.Label( new Rect(150, 280, 300, 30), String.Format("{0} export fail",mapName) );

    }
 
    void MenuNoArg()
    {
        Debug.Log("Selected menu");
    }
 
    void MenuArg1(object obj)
    {
        Debug.Log ("Selected: " + obj);
    }
}